using UnityEngine;
using UnityEngine.UI;

public class PaletteSelectButton : MonoBehaviour
{
	[SerializeField]
	private PaletteScriptable palette;

	[SerializeField]
	private int PaletteID;

	private int price = 1000;

	private bool isRV;

	[SerializeField]
	private GameObject RVIcon;

	[SerializeField]
	private Button PurchaseBtn;

	private PreSetColorController colorController;

	private void Start()
	{
		colorController = Object.FindObjectOfType<PreSetColorController>();
		colorController.OnPalettePurchased += OnPalettePurchased;
		PurchaseBtn.onClick.AddListener(OnButtonClicked);
		if (NintendoPlayerPrefs.GetInt("PaletteUnlock_" + PaletteID, 0) <= 0)
		{
			if (NintendoPlayerPrefs.GetFloat("Money", 0f) < (float)price)
			{
				isRV = true;
				RVIcon.SetActive(value: true);
				PurchaseBtn.gameObject.SetActive(value: false);
			}
		}
		else
		{
			PurchaseBtn.gameObject.SetActive(value: false);
		}
	}

	private void OnDestroy()
	{
		colorController.OnPalettePurchased -= OnPalettePurchased;
	}

	private void OnPalettePurchased(bool withRV)
	{
		if (withRV)
		{
			isRV = false;
			RVIcon.SetActive(value: false);
			if (NintendoPlayerPrefs.GetInt("PaletteUnlock_" + PaletteID, 0) <= 0)
			{
				GetComponent<Button>().interactable = false;
			}
		}
		else if (NintendoPlayerPrefs.GetFloat("Money", 0f) < (float)price && NintendoPlayerPrefs.GetInt("PaletteUnlock_" + PaletteID, 0) <= 0)
		{
			isRV = true;
			RVIcon.SetActive(value: true);
			PurchaseBtn.gameObject.SetActive(value: false);
		}
	}

	public void OnButtonClicked()
	{
		if (NintendoPlayerPrefs.GetInt("PaletteUnlock_" + PaletteID, 0) > 0)
		{
			colorController.SetPalette(palette.colors);
		}
		else if (isRV)
		{
			colorController.SetPalette(palette.colors);
		}
		else if (NintendoPlayerPrefs.GetFloat("Money", 0f) >= (float)price)
		{
			NintendoPlayerPrefs.SetFloat("Money", NintendoPlayerPrefs.GetFloat("Money", 0f) - (float)price);
			NintendoPlayerPrefs.SetInt("PaletteUnlock_" + PaletteID, 1);
			colorController.OnPalettePurchasedEvent(isRV: false);
			PurchaseBtn.gameObject.SetActive(value: false);
		}
	}
}
